package com.ramos.kuaixiao.utils;


import android.content.Context;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.ramos.kuaixiao.R;

public class CommonLoading {

    private Context mContext;
    private android.app.AlertDialog ad;
    private TextView mLoadText;
    private ImageView mLoadImage;
    private Animation mLoadAnim;

    private IDoSomething doSomethingListener;

    public CommonLoading(Context context, boolean canCancelable, IDoSomething doSomethingListener) {
        mContext = context;
        loading(canCancelable);
        this.doSomethingListener = doSomethingListener;
    }

    public CommonLoading(Context context) {
        mContext = context;
        loading(false);
    }

    private void loading(boolean canCancelable) {
        ad = new android.app.AlertDialog.Builder(mContext).create();
        ad.setCancelable(canCancelable);
        ad.setCanceledOnTouchOutside(canCancelable);
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.common_loading);
        mLoadText = (TextView) window.findViewById(R.id.loadtxt);
        mLoadImage = (ImageView) window.findViewById(R.id.loading);
        mLoadAnim = AnimationUtils.loadAnimation(mContext, R.anim.loading);
        mLoadImage.startAnimation(mLoadAnim);
    }

    public void setLoadText(String text) {
        this.mLoadText.setText(text);
    }

    public void setLoadText(int textIds) {
        this.mLoadText.setText(textIds);
    }

    public void dismiss() {
        mLoadImage.clearAnimation();
        if (null != doSomethingListener) {
            doSomethingListener.doSomething();
        }
        ad.dismiss();
    }

}